package com.dp.mallchat.common.user.sensitive;

import java.util.List;

/**
 * @Author: dupeng
 * @CreateTime: 2024-07-24  20:40
 * @Description: 敏感词引导类
 */
public class SensitiveWordsBs {

    private SensitiveWordsBs(){

    }

    //加载敏感词引导类
    public static SensitiveWordsBs newInstance() {
        return new SensitiveWordsBs();
    }

    //加载DFA实例
    private SensitiveWordsFilter sensitiveWordFilter = DFAFilter.getInstance();

    //敏感词源
    private IWordFactory wordDeny;

    /**
     * 过滤策略
     *
     * @param filter 过滤器
     * @return 结果
     * @since 0.7.0
     */
    public SensitiveWordsBs filterStrategy(SensitiveWordsFilter filter) {
        if (filter == null) {
            throw new IllegalArgumentException("filter can not be null");
        }
        this.sensitiveWordFilter = filter;
        return this;
    }

    /**
     * 加载敏感词库
     * @param wordFactory
     * @return
     */
    public SensitiveWordsBs sensitiveWordsBs(IWordFactory wordFactory) {
        if (wordFactory == null) {
            throw new IllegalArgumentException("wordFactory can not be null");
        }
        this.wordDeny = wordFactory;
        return this;
    }

    /**
     * 初始化敏感词
     * @return
     */
    public SensitiveWordsBs init() {
        List<String> words = wordDeny.getSensitiveWords();
        loadSensitiveWords(words);
        return this;
    }

    /**
     * 加载敏感词
     * @param wordList
     */
    public void loadSensitiveWords(List<String> wordList){
        sensitiveWordFilter.loadSensitiveWords(wordList);
    }

    /**
     * 是否为敏感词
     * @param word 内容
     * @return 返回true false
     */
    public boolean isSensitiveWords(String word){
        return sensitiveWordFilter.isSensitiveWords(word);
    }

    /**
     * 过滤敏感词
     * @param content 内容
     * @return 返回过滤后的结果
     */
    public String filter(String content){
        return sensitiveWordFilter.filter(content);
    }

}
